package com.lh.api.controller.shop;

import com.lh.api.controller.AbstractController;
import com.lh.api.service.ActivityShopOneService;
import com.lh.api.service.ShopService;
import com.lh.api.vo.DoboCodeVo;
import com.lh.api.vo.DoboDetailVo;
import com.lh.api.vo.OneDoboVo;
import com.lh.api.vo.ShopPeriodVo;
import com.lh.common.enums.ShopSortTypeEnum;
import com.lh.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gze
 * @ClassName: DoboAction
 * @Description: 一元夺宝
 * @date: 2019-08-14 11:07
 */

@Api(value="DoboAction",tags={"一元夺宝 v1.3"})
@RestController
@RequestMapping("/api/dobo")
public class DoboAction extends AbstractController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ActivityShopOneService activityShopOneService;

    @ApiOperation(value = "夺宝首页接口 v1.3")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "sortType", value = "商品排序类型：DEFAULT:默认 时间排序，PRICEDESC:价格从高到底，PRICEASC:价格从低到高 v1.3", required = false, dataTypeClass = ShopSortTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "qureyTime", value = "查询时间,形如:2020-03-01 21:00:00,查询第一页时不用传，由后端返回", required = false,dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
    })
    @GetMapping("/oneDoboList")
    @ResponseBody
    public Result<OneDoboVo> oneDoboList(String qureyTime, ShopSortTypeEnum sortType, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageNum == 1){
            qureyTime = DateTimeUtil.getDateTime();
        }
//        else {
//            AssertUtil.assertNotNull(qureyTime, Constant.RESULT.FI1000, "QureyTime is required");
//        }
//        PageUtils<OneDoboVo> oneDoboVoPageUtils = shopService.oneDoboList(qureyTime,pageNum, pageSize);
        PageUtils<OneDoboVo> oneDoboVoPageUtils = shopService.oneDoboList(qureyTime, sortType, pageNum, pageSize);
        oneDoboVoPageUtils.setQureyTime(qureyTime);
        return Result.ok(oneDoboVoPageUtils);
    }

    @ApiOperation(value = "夺宝商品详情页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "aid", value = "活动商品id", required = true, dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "pid", value = "期数id", required = false, dataTypeClass= Long.class),
    })
    @GetMapping("/getDoboShopDetail")
    @ResponseBody
    public Result<DoboDetailVo> getDoboShopDetail(Long aid, Long pid){
        AssertUtil.assertNotNull(aid, Constant.RESULT.FI1000, "aid is required");

        DoboDetailVo doboDetailVo = activityShopOneService.getDoboShopDetail(aid,pid);
        return Result.ok(doboDetailVo);
    }

    @ApiOperation(value = "当期夺宝码列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pid", value = "期数id", required = true, dataTypeClass= Long.class),
    })
    @GetMapping("/getDoboCode")
    @ResponseBody
    public Result<List<DoboCodeVo>> getDoboCode(Long pid){
        AssertUtil.assertNotNull(pid, Constant.RESULT.FI1000, "pid is required");
        List<DoboCodeVo> doboCodeVos = activityShopOneService.getDoboCode(pid);
        return Result.ok(doboCodeVos);
    }

    @ApiOperation(value = "期数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "latestPeriod", value = "最新一期期数，查第一页时不传", required = false,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "aid", value = "活动商品id", required = true, dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping("/getPeriodList")
    @ResponseBody
    public Result<ShopPeriodVo> getPeriodList(Integer latestPeriod, Long aid, Integer pageNum, Integer pageSize){
        AssertUtil.assertNotNull(aid, Constant.RESULT.FI1000, "aid is required");
//        if(pageNum != null && pageNum != 1){
//            AssertUtil.assertNotNull(latestPeriod, Constant.RESULT.FI1000, "LatestPeriod is required");
//        }
        Map<String, Object> param = new HashMap<>();
        param.put("aid",aid);
        param.put("latestPeriod",latestPeriod);
        PageUtils<ShopPeriodVo> pageUtils = activityShopOneService.getPeriodList(param, pageNum, pageSize);
        return Result.ok(pageUtils);
    }
}
